<!--
 * @Description: 
 * @Date: 2024-06-25 21:24:10
 * @LastEditTime: 2024-09-18 11:37:21
-->
<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5" type="login">
{
  style: {
    navigationStyle: 'custom',
  },
}
</route>
<template>
  <view class="w-screen h-screen overflow-hidden bg-white pos-relative">
    <view>
      <img src="../../static/images/logo.png" class="w-100% h-600rpx" />
    </view>

    <view class="w-100% px-80rpx box-border pos-relative flex flex-col gap-40rpx mt-25%">
      <wd-button
        block
        open-type="getPhoneNumber"
        @getphonenumber="
          (e) => {
            getphonenumber(e, 'cadre')
          }
        "
      >
        干部登录
      </wd-button>
      <wd-button
        block
        open-type="getPhoneNumber"
        @getphonenumber="
          (e) => {
            getphonenumber(e, 'villager')
          }
        "
      >
        村民登录
      </wd-button>
      <wd-button
        block
        open-type="getPhoneNumber"
        @getphonenumber="
          (e) => {
            getphonenumber(e, 'tourist')
          }
        "
      >
        游客登录
      </wd-button>
      <view
        class="w-100% h-85% pos-absolute top-0 left-0"
        v-if="!privacy"
        @click.self="ifPrivacy"
      ></view>
      <view class="mx-auto my-0 flex">
        <wd-checkbox v-model="privacy"></wd-checkbox>
        <text class="text-24rpx color-#999999">
          已阅读并同意
          <text class="color-#000" @click="agreement">《用户服务协议》</text>
          及
          <text class="color-#000" @click="intimacy">《隐私政策》</text>
        </text>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { loginApi } from '@/service/index/foo'
import { useUserStore } from '@/store'
import { useMessage } from 'wot-design-uni'
const message = useMessage()
const privacy = ref(false)
const loginCode = ref('')
const userStore = useUserStore()
const ifPrivacy = () => {
  message
    .confirm({
      title: '用户协议及隐私政策',
      msg: '请先阅读并同意《用户服务协议》和《隐私政策》',
    })
    .then(() => {
      privacy.value = true
    })
    .catch(() => {
      privacy.value = false
    })
}
const getphonenumber = (e, type) => {
  if (e.errMsg === 'getPhoneNumber:ok' && loginCode.value) {
    loginApi({
      loginCode: loginCode.value,
      phoneCode: e.code,
      encryptedData: e.encryptedData,
      ivStr: e.iv,
      tenantId: 0,
      userType: type,
    }).then((res: any) => {
      const { hint, residentId, isPartyMember, accessToken, name, gridMemberId, phone, userId } =
        res.data

      userStore.setUserInfo({
        hint,
        residentId,
        gridMemberId,
        isPartyMember,
        accessToken,
        name,
        phone,
        type,
        userId,
        isLogined: type === 'tourist' ? 2 : null,
      })
      if (
        hint === '获取用户失败，结果为空, 请先完成村民入驻' ||
        hint === '用户已删除,请先完成村民入驻'
      ) {
        uni.navigateTo({ url: '/pages/Login/villager' })
      } else if (
        hint === '获取用户失败，结果为空, 请先完成干部入驻' ||
        hint === '用户已删除,请先完成干部入驻'
      ) {
        uni.navigateTo({ url: '/pages/Login/cadre' })
      } else {
        uni.switchTab({ url: '/pages/index/index' })
      }
    })
  }
}
const handleSkip = (type) => {
  if (type) {
    uni.navigateTo({ url: '/pages/Login/villager' })
  } else {
    uni.navigateTo({ url: '/pages/Login/cadre' })
  }
}
const agreement = () => {
  uni.navigateTo({ url: '/pages/Login/agreement' })
}
const intimacy = () => {
  uni.navigateTo({ url: '/pages/Login/intimacy' })
}

// 测试 uni API 自动引入
onShow(() => {
  uni.login({
    provider: 'weixin',
    success: function (loginRes) {
      loginCode.value = loginRes.code
    },
  })
})
</script>

<style lang="scss" scoped>
:deep(.wd-button) {
  width: 100% !important;
  height: 50px !important;
  font-size: 34rpx !important;
  font-weight: bold !important;
  border-radius: 30rpx !important;
}
</style>
